package com.core;

import org.apache.ibatis.jdbc.ScriptRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * create by yongli on 2021-12-18 14:44
 * 使用Mybatis执行脚本，代码量也很少
 */

public class RunSql {

    private static final String driver = "com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/core?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    private static final String username = "root";
    private static final String password = "baibai123";
    private static final String dir = "/Users/yongli/Desktop";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        mybatisExec();
    }

    private static void mybatisExec() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        ScriptRunner runner = new ScriptRunner(conn);
        try {
            File di = new File(dir);
            File[] files = di.listFiles();
            for (File file : files) {
                byte[] data = new byte[1024 * 300];
                // >96kB
                try {
                    if (file.getName().endsWith(".sql") && file.length() > 1024 * 96) {
                        System.out.println(file.length());
                        System.out.println(file.getName());
                        runner.setStopOnError(true);
                        FileInputStream fis = new FileInputStream(file);
                        fis.read(data);
                        fis.close();
                        String sql = new String(data);
                        // 掐头去尾
                        int idx1 = 0, idx2;
                        if (!sql.startsWith("INSERT INTO")) {
                            idx1 = sql.indexOf("INSERT INTO");
                        }
                        idx2 = sql.lastIndexOf(");");
                        System.out.println(idx1);
                        System.out.println(idx2);
                        if (idx1 < 0 || idx2 <= idx1) {
                            continue;
                        }
                        sql = sql.substring(idx1, idx2 + 2);
                        System.out.println("执行一个文件");
                        sql = sql.replaceAll("sany_heavy_energy.ts_request_log", "ts_core_log");
                        data = sql.getBytes();
                        FileOutputStream out = new FileOutputStream(file, false);
                        out.write(data);
                        out.close();

                        runner.runScript(new FileReader(file));
                        file.delete();
                    }
                } catch (Exception e) {

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn.close();
    }

}
